package com.cavasmonitor;

import com.cavasmonitor.to.UsuarioTo;
import com.cavasmonitor.util.Session;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class OpcionesActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.opciones);

		Button btn = (Button) findViewById(R.id.btnTemCavas);

		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent i = new Intent(OpcionesActivity.this,
						TemperaturaActivity.class);
				startActivityForResult(i, 1);
			}
		});

		btn = (Button) findViewById(R.id.btnEquipos);

		if (Session.usuario.getRol() == UsuarioTo.Rol.ADMINISTRADOR) {
			btn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					Intent i = new Intent(OpcionesActivity.this,
							EquipoActivity.class);
					startActivityForResult(i, 1);
				}
			});
		} else {
			btn.setVisibility(View.INVISIBLE);
		}

		btn = (Button) findViewById(R.id.btnAlarmas);

		if (Session.usuario.getRol() == UsuarioTo.Rol.ADMINISTRADOR) {
			btn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					Intent i = new Intent(OpcionesActivity.this,
							AlarmasActivity.class);
					startActivityForResult(i, 1);
				}
			});
		} else {
			btn.setVisibility(View.INVISIBLE);
		}

		btn = (Button) findViewById(R.id.btnReportes);

		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent i = new Intent(OpcionesActivity.this,
						ReportesActivity.class);
				startActivityForResult(i, 1);
			}
		});

		ImageButton img = (ImageButton) findViewById(R.id.btnExit);

		img.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {

				new AlertDialog.Builder(OpcionesActivity.this)
						.setTitle(R.string.msg_salir)
						.setMessage(R.string.msg_logout)
						.setPositiveButton(R.string.msg_aceptar,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										Session.usuario = null;
										Intent i = new Intent(
												OpcionesActivity.this,
												MainActivity.class);
										startActivityForResult(i, 1);
									}
								})
						.setNegativeButton(R.string.msg_cancelar, null).show();

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		if (Session.usuario.getRol() == UsuarioTo.Rol.ADMINISTRADOR) {
			getMenuInflater().inflate(R.menu.main, menu);
		} 
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch (item.getOrder()) {
		case Session.M_USUARIOS:
			i = new Intent(OpcionesActivity.this, UsuariosActivity.class);
			startActivityForResult(i, 1);
			return true;
		case Session.M_CFG:
			i = new Intent(OpcionesActivity.this, ConfiguracionActivity.class);
			startActivityForResult(i, 1);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
